package com.htfyun.roobofactory.factorytest;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.StatFs;
import android.support.v4.app.ActivityCompat;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.htfyun.roobofactory.BaseActivity;
import com.htfyun.roobofactory.R;
import com.htfyun.roobofactory.config.AppConfig;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by sst on 2018/7/10.
 */

public class FlashAndMemoryTestActivity extends BaseActivity {

    private Button btnPass;
    private Button btnFail;

    private TextView txtFlashCapacity;
    private TextView txtMemoryCapacity;

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_factorytest_flash_and_memory);

        mContext = this;

        btnPass = (Button) findViewById(R.id.btnPass);
        btnFail = (Button) findViewById(R.id.btnFail);
        btnPass.setOnClickListener(onPassOrFailClickListener);
        btnFail.setOnClickListener(onPassOrFailClickListener);

        txtFlashCapacity = (TextView) findViewById(R.id.txtFlashCapacity);
        txtMemoryCapacity = (TextView) findViewById(R.id.txtMemoryCapacity);


    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, " getTotalInternalMemorySize = " + getTotalInternalMemorySize());
        Log.e(TAG, " getTotalMemorySize = " + getTotalMemorySize());

        txtFlashCapacity.setText(Formatter.formatFileSize(mContext, getTotalInternalMemorySize()) + ", " + getTotalInternalMemorySize() + " Bytes");

        txtMemoryCapacity.setText(Formatter.formatFileSize(mContext, getTotalMemorySize()) + ", " + getTotalMemorySize() + " Bytes");

        if (getTotalInternalMemorySize() >= AppConfig.MIN_FLASH_CAPACITY
                && getTotalMemorySize() >= AppConfig.MIN_MEMORY_CAPACITY
                ) {

            String log = getString(R.string.flash_capacity_en) + txtFlashCapacity.getText()
                    + ", " + getString(R.string.memory_capacity_en) + txtMemoryCapacity.getText();
            FactoryTestItemsEnum.test_flash_and_memory.setLog(mContext, log);

            startAutoPass(R.string.test_flash_and_memory_pass);
        }

    }

    private long getTotalMemorySize() {
        String dir = "/proc/meminfo";
        try {
            FileReader fr = new FileReader(dir);
            BufferedReader br = new BufferedReader(fr, 4096);
            String memoryLine = br.readLine();
            String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
            br.close();
            return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024L;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long totalBlocks = stat.getBlockCountLong();
        return totalBlocks * blockSize;
    }

    private void setBtnPassOrFailEnabled(boolean enabled) {
        btnFail.setEnabled(enabled);
        btnPass.setEnabled(enabled);
    }

    private View.OnClickListener onPassOrFailClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btnPass) {

                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(getActivity());

            } else if (v == btnFail) {

                setResult(RESULT_CANCELED);
                ActivityCompat.finishAfterTransition(getActivity());

            }
        }
    };

}
